<template>
	<view class="">
		<view class="mainpadding2" style="padding-bottom: 150rpx;">
			<view class="ffffff radius mainpadding  margin_top"
				@click="checkaddress(item.id)" v-for="item in list.data" :key="item.id">
				<view class="flexleft">
					<view class="titletext hei_text bold margin_right2">{{item.name}}</view>
					<view class="titletext hei_text bold">{{item.mobile}}</view>
				</view>
				<view class="flexleft xiahuaxian margin_top">
					<u-icon name="map" color="#3478F5" size="16"></u-icon>
					<view class="xiaohei margin_left1">{{item.region}}{{item.address}}</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="flexleft" @click.stop="selectmr(item.id)">
						<view class="nocheckaudio margin_right1" v-show="!item.is_default"></view>
						<view class="checkaudio margin_right1" v-show="item.is_default"></view>
						<view class="xiaohei" v-show="!item.is_default">设为默认</view>
						<view class="xiaohei" v-show="item.is_default">默认地址</view>
					</view>
					<view class="flexright">
						<view class="flexleft margin_right" @click.stop="bianji(item.id)">
							<u-icon name="edit-pen" color="#333" size="16"></u-icon>
							<view class="xiaohei">编辑</view>
						</view>
						<view class="flexleft" @click.stop="deletes(item.id)">
							<u-icon name="trash" color="#333" size="16"></u-icon>
							<view class="xiaohei">删除</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="posibottom mainpadding" @click="Added">
			<view class="submitbtn">+新增收货地址</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				dizhilist: {},
				type: "",
				page: 1,
				list: {
					data: []
				},
			}
		},
		onLoad(options) {
			this.type = options.type
		},
		onShow() {
			this.init();
		},
		methods: {
			checkaddress(id){
				if(this.type==1){
					uni.$emit("addressId",id)
					uni.navigateBack(1)
				}
			},
			init(isPage, page) {
				let _this = this;
				httpRequest.request("/api/address/getlist", 'GET', {
					page: page || 1,
				}, false, false, true).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
							this.pageShow = true
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			selectmr(id) { //地址设置默认
				httpRequest.request('/api/address/setdefault', 'POST', {
					id: id,
				}, false, false, true).then(res => {
					uni.hideLoading() //隐藏loading显示框
					if (res.code == 1) {
						httpRequest.toast("设置成功") //弹框显示设置成功
						this.init();
					} else {
						httpRequest.toast(res.msg)
					}
				})
			},
			//删除接口
			deletes(id) {
				httpRequest.modal('提示', '确认删除当前数据吗？', true, (res) => {
					if (res) {
						httpRequest.request('/api/address/del', 'POST', {
							id: id
						}, false, false, true).then(res => {
							if (res.code == 1) {
								httpRequest.toast(res.msg)
								this.init();
							} else {
								httpRequest.toast(res.msg)
							}
						})
					}
				})
			},
			bianji(id) { //编辑跳转
				uni.navigateTo({
					url: '/pages_mine/tianjiashdz?id=' + id
				})
			},
			Added() { //添加地址
				uni.navigateTo({
					url: '/pages_mine/tianjiashdz'
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.gudingdb {
		background-color: #fff;
	}

	.kym {
		margin: 250rpx auto 0;
		width: 319rpx;
		height: 267rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.weigouxuan {
		width: 22rpx;
		border-radius: 50%;
		height: 22rpx;
		border: 1rpx solid #999999;
	}

	.yigouxuan {
		width: 24rpx;
		height: 24rpx;
	}

	.xiahuaxian {
		padding-bottom: 30rpx;
		border-bottom: 1rpx solid rgba(153, 153, 153, .1);
	}
</style>
